package com.plian.system.controller.transfer;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.plian.system.config.AppKeySign;
import com.plian.system.constant.sys.FormStatusConstant;
import com.plian.system.dto.transfer.DataPlatSign;
import com.plian.system.entity.fr.bank.BankKey;
import com.plian.system.entity.fr.bonds.BondsApply;
import com.plian.system.entity.fr.bonds.BondsIssue;
import com.plian.system.entity.fr.bonds.BondsRepayment;
import com.plian.system.model.R;
import com.plian.system.model.token.PassToken;
import com.plian.system.service.cqzc.bondissue.BondsIssueService;
import com.plian.system.service.cqzc.bondsapply.BondsApplyService;
import com.plian.system.service.cqzc.bondsrepayment.BondsRepaymentService;
import com.plian.system.service.fr.bank.IBankKeyService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "api/bondstransfer")
@Api(value = "债卷数据对接", tags = "债卷数据对接")
public class BondsTransferController {

    @Autowired
    private BondsApplyService bondsApplyService;

    @Autowired
    private BondsIssueService bondsIssueService;

    @Autowired
    private BondsRepaymentService bondsRepaymentService;

    @Autowired
    private IBankKeyService bankKeyService;

    @PostMapping(value = "/bonds-apply-list")
    @PassToken
    @AppKeySign
    public R<List<BondsApply>> bondsApplyList(@RequestBody DataPlatSign dataPlatSign){
        String appId = dataPlatSign.getAppId();
        BankKey bankKey = bankKeyService.getOne(Wrappers.<BankKey>lambdaQuery().eq(BankKey::getAppId, appId));
        String topGroupId = bankKey.getTopGroupId();
        return R.data(bondsApplyService.list(Wrappers.<BondsApply>lambdaQuery()
                .eq(BondsApply::getOrgId, topGroupId)
                .eq(BondsApply::getStatus, FormStatusConstant.FINISH)));
    }

    @PostMapping(value = "/bonds-issue-list")
    @PassToken
    @AppKeySign
    public R<List<BondsIssue>> bondsIssueList(@RequestBody DataPlatSign dataPlatSign){
        String appId = dataPlatSign.getAppId();
        BankKey bankKey = bankKeyService.getOne(Wrappers.<BankKey>lambdaQuery().eq(BankKey::getAppId, appId));
        String topGroupId = bankKey.getTopGroupId();
        return R.data(bondsIssueService.list(Wrappers.<BondsIssue>lambdaQuery()
                .eq(BondsIssue::getOrgId, topGroupId)
                .eq(BondsIssue::getStatus, FormStatusConstant.FINISH)));
    }

    @PostMapping(value = "/bonds-repayment-list")
    @PassToken
    @AppKeySign
    public R<List<BondsRepayment>> bondsRepaymentList(@RequestBody DataPlatSign dataPlatSign){
        String appId = dataPlatSign.getAppId();
        BankKey bankKey = bankKeyService.getOne(Wrappers.<BankKey>lambdaQuery().eq(BankKey::getAppId, appId));
        String topGroupId = bankKey.getTopGroupId();
        return R.data(bondsRepaymentService.list(Wrappers.<BondsRepayment>lambdaQuery()
                .eq(BondsRepayment::getOrgId, topGroupId)
                .eq(BondsRepayment::getStatus, FormStatusConstant.FINISH)));
    }
}
